import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;

public class Registers {

	HashMap<String, Integer> registers;
	static HashMap<String, String> binaryRegisters;

	public Registers() {
		this.registers = new HashMap<String, Integer>();
		this.binaryRegisters = new HashMap<String, String>();
		generateBinary();
		this.generate();
	}

	public Registers(HashMap<String, String> preAssignedRegisters) {
		this();
		Set<String> keys = preAssignedRegisters.keySet();
		Iterator iter = keys.iterator();

		while (iter.hasNext()) {
			String key = (String) iter.next();
			String val = preAssignedRegisters.get(key);
			int value = Integer.parseInt(val);
			System.out.println(value);
			this.registers.put(key, value);
		}
	}
	
	public int[] getArray() {
		Set<String> keys = registers.keySet();
		Iterator iter = keys.iterator();
		int[] out = new int[32];
		int i = 0;
		while (iter.hasNext()) {
			String key = (String) iter.next();
			int value = registers.get(key).intValue();
			out[i++] = value;
		}
		return out;
	}
	
	public void putArray(int[] values) {
		Set<String> keys = registers.keySet();
		Iterator iter = keys.iterator();
		int i = 0;
		while (iter.hasNext()) {
			String key = (String) iter.next();
			int value = values[i];
			this.registers.put(key, value);
		}
	}

	public int read(String reg) {
		System.out.println("Read>>>>" + reg);
		System.out.println("READ>>>>>"+registers.get(reg));
		return registers.get(reg);
	}

	public boolean write(String register, int value) {
		if (registers.keySet().contains(register)) {
			System.out.println(register + ">>>" + value);
			registers.put(register, value);
			return true;
		}
		return false;
	}
	 public boolean isReg(String key) {
		 return this.registers.containsKey(key);
	 }
	public void generate() {
		final int zero = 0;
		registers.put("$zero", zero);
		registers.put("$at", 0);
		registers.put("$v0", 0);
		registers.put("$v1", 0);
		registers.put("$a0", 0);
		registers.put("$a1", 0);
		registers.put("$a2", 0);
		registers.put("$a3", 0);
		registers.put("$t0", 0);
		registers.put("$t1", 0);
		registers.put("$t2", 0);
		registers.put("$t3", 0);
		registers.put("$t4", 0);
		registers.put("$t5", 0);
		registers.put("$t6", 0);
		registers.put("$t7", 0);
		registers.put("$s0", 0);
		registers.put("$s1", 0);
		registers.put("$s2", 0);
		registers.put("$s3", 0);
		registers.put("$s4", 0);
		registers.put("$s5", 0);
		registers.put("$s6", 0);
		registers.put("$s7", 0);
		registers.put("$t8", 0);
		registers.put("$t9", 0);
		registers.put("$k0", 0);
		registers.put("$k1", 0);
		registers.put("$gp", 0);
		registers.put("$sp", 0);
		registers.put("$fp", 0);
		registers.put("$ra", 0);
	}
	
	public static void generateBinary() {
		binaryRegisters = new HashMap<String, String>();
		binaryRegisters.put("$zero", "00000");
		binaryRegisters.put("$at", "00001");
		binaryRegisters.put("$v0", "00010");
		binaryRegisters.put("$v1", "00011");
		binaryRegisters.put("$a0", "00100");
		binaryRegisters.put("$a1", "00101");
		binaryRegisters.put("$a2", "00110");
		binaryRegisters.put("$a3", "00111");
		binaryRegisters.put("$t0", "01000");
		binaryRegisters.put("$t1", "01001");
		binaryRegisters.put("$t2", "01010");
		binaryRegisters.put("$t3", "01011");
		binaryRegisters.put("$t4", "01100");
		binaryRegisters.put("$t5", "01101");
		binaryRegisters.put("$t6", "01110");
		binaryRegisters.put("$t7", "01111");
		binaryRegisters.put("$s0", "10000");
		binaryRegisters.put("$s1", "10001");
		binaryRegisters.put("$s2", "10010");
		binaryRegisters.put("$s3", "10011");
		binaryRegisters.put("$s4", "10100");
		binaryRegisters.put("$s5", "10101");
		binaryRegisters.put("$s6", "10110");
		binaryRegisters.put("$s7", "10111");
		binaryRegisters.put("$t8", "11000");
		binaryRegisters.put("$t9", "11001");
		binaryRegisters.put("$k0", "11010");
		binaryRegisters.put("$k1", "11011");
		binaryRegisters.put("$gp", "11100");
		binaryRegisters.put("$sp", "11101");
		binaryRegisters.put("$fp", "11110");
		binaryRegisters.put("$ra", "11111");
	
	}

	public static String getBinRep(String reg) {
		return binaryRegisters.get(reg);
	}
	
	public void setRegFile(HashMap<String, Integer> values) {
		Set<String> keys = values.keySet();
		Iterator iter = keys.iterator();
		while (iter.hasNext()) {
			String key = (String) iter.next();
			Integer val = values.get(key);
			this.registers.put(key, val);
		}
	}
	
}
